////
////  HomeViewController.swift
////  SinaWeibo
////
////  Created by 张子国 on 15/11/8.
////  Copyright © 2015年 张子国. All rights reserved.
////
//
import UIKit
import AFNetworking
import SVProgressHUD

//定义重用cell标识符
private let homeCellID = "homeCell"
class HomeViewController: BaseTableViewController {
    private lazy var statuses = [Status]()
    override func viewDidLoad() {
        super.viewDidLoad()
        if !userLogin {
        visitorLoginView?.setUIInfo(nil, title: "关注一些人，回这里看看有什么惊喜")
            return
        }
        
        //将模型进行展示
        self.prepareTableView()
        loadData()
        
        
        
}
    
 @objc private func loadData() {
    
//    let since_id = statuses.first?.id ?? 0
//    let max_id = statuses.last?.id ?? 0
    
    var since_id = 0
    var max_id = 0
    
    if indicatorView.isAnimating() {
        since_id = 0
        max_id = statuses.last?.id ?? 0
    } else {
        max_id = 0
        since_id = statuses.first?.id ?? 0
    }

    StatusListViewModel.loadData(since_id,max_id: max_id) { (array) -> () in
        print(array?.count)
        self.refreshControl?.endRefreshing()
        if array == nil {
            //停止动画
            self.indicatorView.stopAnimating()
            SVProgressHUD.showErrorWithStatus(AppErrprlip)
            return
        }
        
        if since_id > 0 {
            self.statuses = array! + self.statuses
        } else if max_id > 0 {
            self.statuses += array!
            self.indicatorView.stopAnimating()
        } else {
            self.statuses = array!
        }
        
        
        //刷新表格
        self.tableView.reloadData()
        
    }

    }
    //MARK: 懒加载上拉刷新
    lazy private  var indicatorView : UIActivityIndicatorView = {
        let indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
        return indicator
    }()

     private func prepareTableView() {
    //设置下拉刷新
     refreshControl = UIRefreshControl()
    //添加监听方法
     refreshControl?.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
    
        //注册 cell
      tableView.registerClass(StatusCell.self, forCellReuseIdentifier: homeCellID)

        //自定义行高首先设置预估行高
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableViewAutomaticDimension   //再去修改约束
        //分割线
        tableView.separatorStyle = .None
    }
}

    //MARK:  数据源方法
    extension HomeViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.statuses.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView .dequeueReusableCellWithIdentifier(homeCellID, forIndexPath: indexPath) as! StatusCell
        cell.status = self.statuses[indexPath.row]
        if indexPath.row == statuses.count - 1 && !indicatorView.isAnimating() {
            indicatorView.startAnimating()
            loadData()
        }
        
        
        
//      print(self.statuses[indexPath.row].retweeted_status?.text)
        return cell
        }
    }


